package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrderVo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.educenter.vo.RegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-13
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登陆
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //实现登陆后返回一个token值
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        //实现登陆后返回一个token值
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据token获取信息
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }


    /**
     * 根据用户id获取用户信息  (订单服务)
     */
    @GetMapping("getMemberInfoById/{id}")
    public UcenterMemberOrderVo getMemberInfoById(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrderVo ucenterMemberOrderVo = new UcenterMemberOrderVo();
        BeanUtils.copyProperties(member,ucenterMemberOrderVo);
        return ucenterMemberOrderVo;
    }

    /**
     * 获取某一天的注册人数
     */
    @GetMapping("getRegister/{day}")
    public R getRegister(@PathVariable String day) {
        int count = memberService.getRegisterCount(day);

        return R.ok().data("count",count);
    }
}

